using System;

namespace Pairingproject2._3.Fractions
{
    public class Fraction//分数类
    {
        int numerator; //分子
        int denominator; //分母
        public Fraction(int x, int y)
        {
            numerator = x;
            denominator = y;
        }
        public int GetNumerator()//返回分子
        {
            return numerator;
        }

        public int GetDenominator()//返回分母
        {
            return denominator;
        }

        public static int FracionGetGCD(int a, int b)//判断分子分母不能为
        {
            if(b == 0 || a == 0) return 1;
            return (a % b == 0) ? b : FracionGetGCD(b, a % b);
        }

        public static Fraction operator +(Fraction A, Fraction B)//计算两个分数//operator运算符重载
        {
            int x, y, z = 1;
            x = A.numerator * B.denominator + A.denominator * B.numerator;
            y = A.denominator * B.denominator;
            z = FracionGetGCD(x, y);
            return new Fraction(x / z, y / z);
        }

        public static Fraction operator -(Fraction A, Fraction B)//减
        {
            int x, y, z = 1;
            x = A.numerator * B.denominator - A.denominator * B.numerator;
            y = A.denominator * B.denominator;
            z = FracionGetGCD(x, y);
            return new Fraction(x / z, y / z);
        }

        public static Fraction operator *(Fraction A, Fraction B)
        {
            int x, y, z = 1;
            x = A.numerator * B.numerator;
            y = A.denominator * B.denominator;
            z = FracionGetGCD(x, y);
            return new Fraction(x / z, y / z);
        }

        public static Fraction operator /(Fraction A, Fraction B)
        {
            int x, y, z = 1;
            x = A.numerator * B.denominator;
            y = A.denominator * B.numerator;
            z = FracionGetGCD(x, y);
            return new Fraction(x / z, y / z);
        }

        public static Fraction operator ^(Fraction A, Fraction B)
        {
            int x = 1, y = 1, xx, yy, z = 1, t = 0;
            t = B.numerator / B.denominator;
            xx = A.numerator;
            yy = A.denominator;
            if (yy != 0 && t == 0)
            {
                return new Fraction(1, 1);
            }
            else if (B.numerator / B.denominator == 1)
            {
                return new Fraction(A.numerator, A.denominator);
            }
            else
            {
                while (t > 0)
                {
                    x = x * xx;
                    y = y * yy;
                    t--;
                }
                z = FracionGetGCD(x, y);
                return new Fraction(x / z, y / z);
            }
        }

        public static bool operator <(Fraction A, Fraction B)
        {
            int x, y;
            bool z;
            x = A.numerator * B.denominator - A.denominator * B.numerator;
            y = A.denominator * B.denominator;
            z = (x > 0 && y < 0) || (x < 0 && y > 0);
            return z;
        }

        public static bool operator >(Fraction A, Fraction B)
        {
            int x, y;
            bool z;
            x = A.numerator * B.denominator - A.denominator * B.numerator;
            y = A.denominator * B.denominator;
            z = (x > 0 && y < 0) || (x < 0 && y > 0);
            return z;
        }

        public static bool operator ==(Fraction A, Fraction B)
        {
            Fraction C;
            bool z = false;
            C = A / B;
            if (C.numerator / C.denominator == 1) z = true;
            return z;
        }
        public static bool operator !=(Fraction A, Fraction B)
        {
            Fraction C;
            bool z = false;
            C = A / B;
            if (C.numerator / C.denominator != 1) z = true;
            return z;
        }
    }
}


